<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Questions manager
 *
 * @author	Tran Van Thanh
 * @email	thanhtran@vietnambiz.com
 * @date	01.09.2011
 */
require_once('admin'.EXT);

class Questions extends Admin {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('text');
		$this->load->model(array('questions_model'));		
	}

	function index()
	{
		$page = (isset($_GET['p']) && is_numeric($_GET['p'])) ? $_GET['p'] : 1;

        if(isset($_GET['status'])) {
            $status = ($_GET['status'] == 'active') ? 'yes' : 'no';
            $this->db->where('status', $status);
        }
		if(isset($_GET['change_usually'])) {
            $status = ($_GET['change_usually'] == 'active') ? 'yes' : 'no';
            $this->db->where('usually', $status);
        }
		if(isset($_GET['keyword'])) {    
			$keyword = $_GET['keyword'];
			$keyword = strip_tags($keyword);
			if($keyword == 'keyword')
			{
				$keyword = "";
			}
			else
			{
				$this->db->like('title', $keyword)
            		 ->or_like('contents', $keyword);            		 
			}
        }

		$this->data['questions'] 	= $this->questions_model->get_questions($page != "" ? $page : 1);		
		$this->data['page'] 		= $page;

    	$conf =	array(
    		'base_url'		=> admin_url($this->router->class).'/',
    		'total_rows'	=> $this->questions_model->count_all(),
    		'per_page'		=> $this->config->item('questions', 'limit'),
    		'cur_page'		=> $page,
    	);
    	$this->pagination->initialize($conf);
    	$this->data['pagnav'] = $this->pagination->display_query_string();

		if($this->input->is_ajax_request()) {
			$this->load->view('questions/list', $this->data);
		}
		else {
			$this->data['tpl_file']	= 'questions/index';
			$this->load->view('layout/default', $this->data);
		}
	}	

	function edit($question_id = null)
	{		
		$questions = $this->questions_model->get_questions_info($question_id);		
		
		if($_SERVER['REQUEST_METHOD'] == 'POST') 
		{
    		$this->form_validation->set_rules('txt_title', 'Tiêu đề câu hỏi', 'required|trim|xss_clean');    		                      

    		if($this->form_validation->run() == TRUE)
            {       
            	//Update products type
            	$title = $this->input->post('txt_title');
				$contens = $this->input->post('txt_answer');
				
            	$arr = array(
            		'title'				=> $title,
            		'title_link'		=> ascii_link($title,'-'),
            		'contents'			=> $contens,            		
            		'usually'			=> ($this->input->post('cck_usually') == 'on') ? 'yes' : 'no',
            		'status'			=> $this->input->post('status')
            	);            	
            	            	
            	$this->questions_model->update($question_id,$arr);
            	die('yes');            	            	            	
            }
            else {
            	die(validation_errors());
            }
		}

		$this->data['questions'] 	= $questions;		
		$this->load->view('questions/edit', $this->data);
	}

	function answers($question_id = null)
	{		
		$questions = $this->questions_model->get_questions_info($question_id);
		$answer_info = $this->questions_model->get_answer_info_by_id_questions($question_id);
		
		if($_SERVER['REQUEST_METHOD'] == 'POST') 
		{			
			if($answer_info == null) //Neu chua ton tai ID_question trong bang answer roi thi cap nhat moi 
			{
				$arr = array(
	            	'id_question'		=> $question_id,
	            	'contents'			=> $this->input->post('txt_answer'),
					'date_answer'		=> date('Y-m-d H:i:s'),
	            	'status'			=> $this->input->post('status')
	            );
	            $re = $this->db->insert('answers', $arr);
	            
	            $id_product =  strip_tags($this->input->post('txt_id_product'));
				$arr_id_products = explode(",",$id_product);
            	foreach($arr_id_products as $k => $v)
            	{
            		$arr_data = array(
            			'id_question'	=> $question_id,
            			'id_product'	=> $v
            		);	 
            		$this->db->insert('questions_products', $arr_data);           		
            	}
            		
	            die('yes'); 
			}           	            	  
			else if($answer_info != null)//Neu da ton tai thi update 
			{				
				$arr = array(
	            	'id_question'		=> $question_id,
	            	'contents'			=> $this->input->post('txt_answer'),
					'date_answer'		=> date('Y-m-d H:i:s'),
	            	'status'			=> $this->input->post('status')
	            );	            
	            $re = $this->db->where('id', $answer_info[0]->id)->update('answers', $arr);
				
	            //Delete all where id_question into table questions_products
            	$this->db->delete('questions_products', array('id_question' => $question_id));
            	$id_product =  strip_tags($this->input->post('txt_id_product'));
            	$arr_id_products = explode(",",$id_product);            	
            	foreach($arr_id_products as $k => $v)
            	{
            		$arr_data = array(
            			'id_question'	=> $question_id,
            			'id_product'	=> $v
            		);	 
            		$this->db->insert('questions_products', $arr_data);           		
            	}
            	
	            die('yes'); 
			}
		}
		$this->data['questions'] 	= $questions;	
		$this->data['answer_info']	= $answer_info;	
		$this->load->view('questions/answers', $this->data);
	}
	
	/** 
	 * Delete product type
	 * @author Tran Van Thanh
	 * @date 30.08.2011
	 */
    function delete($question_id = null)
    {    	
        if($_SERVER['REQUEST_METHOD'] == 'POST') 
        {        	
            $this->questions_model->delete_question($question_id);         
            die('yes');
        }
    }

	function do_action()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST') {

			$id_list = $this->input->post('id');
			$action = $this->input->post('action');

			if($action == 'delete') {
				$this->delete($id_list);
			}
			elseif($action == 'pending') {
				$this->questions_model->update_many($id_list, array('status' => 'no'));
			}
			elseif($action == 'active') {
				$this->questions_model->update_many($id_list, array('status' => 'yes'));
			}

			die('yes');
		}
	}

	function change_status()
	{
		$question_id = $this->input->post('question_id');
		$status = $this->input->post('status');		

		$this->questions_model->update($question_id, array('status' => $status));
		die('yes');
	}
	
	function change_usually()
	{
		$question_id = $this->input->post('question_id');
		$status = $this->input->post('status');

		$this->questions_model->update($question_id, array('usually' => $status));
		die('yes');
	}	

	function load_row($question_id = null)
	{				
		$this->data['questions'] 	= $this->questions_model->get($question_id);
		$this->data['answer_info'] 	= $this->questions_model->get_answer_info_by_id_questions($question_id);			
		$this->load->view('questions/row', $this->data);
	}	
}

